package switchviz.recording;

import switchviz.core.data.Data;
import switchviz.core.data.TimeStamp;
import switchviz.core.signal.ISignal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;


public class ReplayFileAdapter
{
    //~ Instance fields ----------------------------------------------------------------------------

    private File _file;
    private String _signalname;
    private BufferedReader _reader = null;
    private List<ISignal> _signals = new ArrayList<ISignal>();
    private long _timestamp;
    private Data data = new Data();

    //~ Constructors -------------------------------------------------------------------------------

    public ReplayFileAdapter(String signal, File file)
    {
        _signalname = signal;
        _file = file;
    }

    //~ Methods ------------------------------------------------------------------------------------

    public void close() throws IOException
    {
        if (_reader != null)
        {
            _reader.close();
            _reader = null;
        }
    }

    public boolean open() throws FileNotFoundException
    {
        if (_signals.size() != 0)
        {
            _reader = new BufferedReader(new FileReader(_file));

            return true;
        }

        return false;
    }

    public void addSignal(ISignal signal)
    {
        Data data = new Data();

        data.setValue(0, new TimeStamp(0));

        signal.setData(data);

        _signals.add(signal);
    }

    public void read(long timestamp) throws IOException
    {
        if (_reader != null)
        {
            while (_timestamp < timestamp)
            {
                Iterator<ISignal> iter = _signals.iterator();

                while (iter.hasNext())
                    iter.next().setData(data);

                String line;

                line = _reader.readLine();

                if (line == null)
                {
                	close();
                    return;
                }

                StringTokenizer st = new StringTokenizer(line);

                _timestamp = Long.parseLong(st.nextToken());
                data.setValue(Long.parseLong(st.nextToken()), new TimeStamp(_timestamp));
            }
        }
    }

    public void reset() throws IOException
    {
        data.setValue(0, new TimeStamp(0));

        Iterator<ISignal> iter = _signals.iterator();

        while (iter.hasNext())
            iter.next().setData(data);
    }

    public void restart() throws IOException
    {
        _timestamp = 0;

        if (_reader != null)
            close();
        
        open();
    }

    public String getSignalName()
    {
        return _signalname;
    }

    public String getFileName()
    {
        return _file.toString();
    }

    public int getAttachedSignals()
    {
        return _signals.size();
    }
}
